package com.abaaba.project2.service.serviceImpl;

import com.abaaba.project2.bean.Order;
import com.abaaba.project2.bean.OrderExample;
import com.abaaba.project2.bean.vo.WxUserIndexOrderVo;
import com.abaaba.project2.bean.vo.WxUserIndexVo;
import com.abaaba.project2.mapper.OrderMapper;
import com.abaaba.project2.service.WxUserService;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class WxUserServiceImpl implements WxUserService {
    @Autowired
    OrderMapper orderMapper;

    @Override
    public WxUserIndexVo getUserIndex(Integer userId) {
        // 根据用户 id 获取所有订单
        OrderExample orderExample = new OrderExample();
        OrderExample.Criteria criteria = orderExample.createCriteria();
        criteria.andUserIdEqualTo(userId).andDeletedEqualTo(false);
        List<Order> orders = orderMapper.selectByExample(orderExample);

        // 待付款  101
        // 待发货  201
        // 待收货  301
        Integer unrecv = 0;
        Integer uncomment = 0;
        Integer unpaid = 0;
        Integer unship = 0;

        for (Order order : orders) {
            int orderStatus = order.getOrderStatus();
            if (orderStatus == 101) {
                unpaid++;
            } else if (orderStatus == 201) {
                unship++;
            } else if (orderStatus == 301 ) {
                unrecv++;
            }
            uncomment += order.getComments();
        }

        WxUserIndexOrderVo wxUserIndexOrderVo = new WxUserIndexOrderVo();
        wxUserIndexOrderVo.setUncomment(uncomment);
        wxUserIndexOrderVo.setUnpaid(unpaid);
        wxUserIndexOrderVo.setUnrecv(unrecv);
        wxUserIndexOrderVo.setUnship(unship);

        WxUserIndexVo wxUserIndexVo = new WxUserIndexVo();
        wxUserIndexVo.setOrder(wxUserIndexOrderVo);
        return wxUserIndexVo;
    }
}
